<?php
if ( IPB_THIS_SCRIPT != 'admin' )
{
	print "<h1>Incorrect access</h1>You cannot access this file directly. If you have recently upgraded, make sure you upgraded 'init.php'.";
	exit();
}

class ad_item
{
	var $dropdown = array();
	
	var $ipsclass;
	
	function auto_run()
	{
		if(count($this->ipsclass->cache['market_category']) == 0)
		{
			$this->ipsclass->admin->error("ERROR: You must add a category first");
		}
		
		$code_redirect = array(
								'list' => 'list_items',
								'add' => 'add_item_list',
								'process' => 'process',
								'add_form' => 'add_item_form',
								'add_item' => 'add_item_finish',
								'edit' => 'mass_edit_form',
								'edit_item' => 'mass_edit_save',
								'delete' => 'delete_form',
								'delete_item' => 'delete_item',
								'edits' => 'view_file_edits',
								'quick' => 'list_items',
								'save' => 'quick_save_items',
							);
		if(!$code_redirect[$this->ipsclass->input['do']])
		{
			$this->ipsclass->input['do'] = 'list';
		}
		$run = $code_redirect[$this->ipsclass->input['do']];
		$this->$run();		
	}
	
	function list_items()
	{
		$this->ipsclass->admin->page_title  = "Welcome to managing the ibMarket items";
		$this->ipsclass->admin->page_detail = "Welcome to managing the ibMarket items. Here you can edit or delete items from certain categories.";
		$this->ipsclass->admin->nav[] 		= array( $this->ipsclass->form_code.'&amp;code=category', 'List category' );
		$this->ipsclass->admin->nav[] 		= array( ''                                             , 'List Items'    );
		
		$this->ipsclass->input['cat'] = $this->ipsclass->input['cat'] ? $this->ipsclass->input['cat'] : -1;
		
		$cats = $this->ipsclass->input['cat'] == -1 ? array() : array($this->ipsclass->input['cat']);
		if($this->ipsclass->input['cat'] == -1 || intval($this->ipsclass->input['subs']) == 1)
		{
			$cats = array_merge($cats,$this->_build_sub_cat_array($this->ipsclass->input['cat']));
		}
		
		$this->ipsclass->html .= $this->ipsclass->adskin->start_form( array(
																	 	  	 1 => array( 'section'   , 'components'  ),
																			 2 => array( 'act'       , 'market'      ),
																			 3 => array( 'code'      , 'item'        ),
																			 4 => array( 'do'        , $this->ipsclass->input['do'] == 'quick' ? 'save' : 'edit'),
																			 5 => array( 'cat'       , $this->ipsclass->input['cat']),
																			 6 => array( 'subs'      , $this->ipsclass->input['subs']),
																	)		);
		
		$this->ipsclass->adskin->td_header[] = array( "Icon"               , "1%" );
		$this->ipsclass->adskin->td_header[] = array( "Category"           , ""   );
		$this->ipsclass->adskin->td_header[] = array( "Name & Description" , ""   );
		if($this->ipsclass->input['do'] == 'quick')
		{
			$this->ipsclass->adskin->td_header[] = array( "Stock"          , ""   );
			$this->ipsclass->adskin->td_header[] = array( "Price"          , ""   );
		}
		else
		{
			$this->ipsclass->adskin->td_header[] = array( "Delete"         , ""   );
			$this->ipsclass->adskin->td_header[] = array( "Edit"           , ""   );
		}
		
		$this->ipsclass->html .= $this->ipsclass->adskin->start_table("<div style='float:right'>".$this->ipsclass->adskin->js_make_button("Toggle Quick Edit",$this->ipsclass->base_url."&".$this->ipsclass->form_code_js."&code=item&do=".($this->ipsclass->input['do'] == 'quick' ? 'list' : 'quick')."&cat=".$this->ipsclass->input['cat']."&subs=".$this->ipsclass->input['subs'])."</div>Items");
		
		foreach($cats as $cat_id)
		{
			if(is_array($this->ipsclass->market_class->item_by_cat[$cat_id]) and count($this->ipsclass->market_class->item_by_cat[$cat_id]))
			{
				foreach($this->ipsclass->market_class->item_by_cat[$cat_id] as $i_id => $item)
				{
					$row = array();
					$row[] = "<img src='".$this->ipsclass->market_class->image_url_path."/icons/".$item['icon']."' border='0' alt='Item Icon' />";
					$row[] = $this->ipsclass->cache['market_category'][$item['category']]['cat_name'];
					$row[] = "<b>".$item['item_name']."</b><br />".$this->ipsclass->parser->pre_display_parse($this->ipsclass->parser->pre_db_parse($this->ipsclass->market_class->items_loaded[$item['item_id']]->parse_store_description()));
					if($this->ipsclass->input['do'] == 'quick')
					{
						$row[] = $this->ipsclass->adskin->form_simple_input("item[".$i_id."][stock]", $item['stock'] < 0 ? 'infinite' : $item['stock'], 5);
						$row[] = $this->ipsclass->numbers->currency_format($this->ipsclass->adskin->form_simple_input("item[".$i_id."][base_price]", $item['base_price'],10));
					}
					else
					{
						$row[] = "<center>".$this->ipsclass->adskin->js_make_button("Delete",$this->ipsclass->base_url."&".$this->ipsclass->form_code_js."&code=item&do=delete&item=".$item['item_id'])."</center>";
						$row[] = "<center>".$this->ipsclass->adskin->form_checkbox("item[".$item['item_id']."]")."</center>";
					}
					$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row($row);
				}
			}
		}
		
		$this->ipsclass->html .= $this->ipsclass->adskin->end_form($this->ipsclass->input['do'] == 'quick' ? "Save Prices/Stock" : "Edit Items");
		
		$this->ipsclass->html .= $this->ipsclass->adskin->end_table();
		
		$this->ipsclass->admin->output();
	}
	
	function _build_sub_cat_array($id = -1)
	{
		$ids = array();
		if(is_array($this->ipsclass->market_class->category_tree[$id]) and count($this->ipsclass->market_class->category_tree[$id]))
		{
			foreach(array_keys($this->ipsclass->market_class->category_tree[$id]) as $cat_id)
			{
				$ids[] = $cat_id;
				$ids = array_merge($ids,$this->_build_sub_cat_array($cat_id));
			}
		}
		return $ids;
	}
	
	function add_item_list()
	{
		$this->ipsclass->admin->page_title  = "Welcome to adding/viewing file edits of a ibMarket item";
		$this->ipsclass->admin->page_detail = "Welcome to adding/viewing file edits of a ibMarket item. Here you pick which item you want to add to ibMarket or view the file edits.";
		$this->ipsclass->admin->nav[] 		= array( $this->ipsclass->form_code.'&amp;code=item&amp;do=add', 'Choose Item' );
		
		$this->ipsclass->html .= $this->ipsclass->adskin->start_form( array(
																	 	  	 1 => array( 'section'   , 'components'  ),
																			 2 => array( 'act'       , 'market'      ),
																			 3 => array( 'code'      , 'item'        ),
																			 4 => array( 'do'        , 'process'    ),
																	)		);
		
		$this->ipsclass->adskin->td_header[] = array( "" , "" );
		$this->ipsclass->adskin->td_header[] = array( "" , "" );
		
		$this->ipsclass->html .= $this->ipsclass->adskin->start_table("Which item to add/view?");
		
		$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row(array(
																			"What to do",
																			$this->ipsclass->adskin->form_dropdown('action', array(array('add','Add Item'),array('file','View File Edits'))),
																	)	);
		
		$this->ipsclass->html .= $this->ipsclass->adskin->add_td_basic('Choose which item?', 'left', 'tablesubheader');
		
		$dirs = $this->ipsclass->market_class->get_dir_list(ROOT_PATH."sources/market/items");
		$dirs = array_merge(array(""),$dirs);
		
		foreach($dirs as $dir)
		{
			$title = "";
			$desc = "";
			if(!file_exists(ROOT_PATH."sources/market/items/".$dir."/info.php"))
			{
				continue;
			}
			require(ROOT_PATH."sources/market/items/".$dir."/info.php");
			if(!$title)
			{
				continue;
			}
			$files = $this->ipsclass->market_class->get_file_list(ROOT_PATH."sources/market/items/".$dir,0);
			$drop_down = array(array(-1,"-----------------------"));
			foreach($files as $item)
			{
				if(strstr($item,"item_") != $item || substr($item,-4) != '.php')
				{
					continue;
				}
				
				$class_name = str_replace(".php","",$item);
				
				require_once(ROOT_PATH."sources/market/items/".$dir."/".$item);
				$item_obj = new $class_name;
				
				if(is_subclass_of($item_obj,'template_item'))
				{
					$see = $item_obj->default_name;
					if(!$item_obj->allow_multiple and isset($this->ipsclass->market_class->item_by_folder[$dir."/".$item]))
					{
						$see .= "(View Edits Only)";
					}
					
					$drop_down[] = array($dir."/".$item,$see);
				}
			}
			
			$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row(array(
																				"<b>".$title.":</b>".($desc ? "<br /><i>".$desc."</i>" : ""),
																				$this->ipsclass->adskin->form_dropdown('items['.$dir.']', $drop_down, -1),
																		)	);
		}
		
		$this->ipsclass->html .= $this->ipsclass->adskin->end_form("Add Item");
		
		$this->ipsclass->html .= $this->ipsclass->adskin->end_table();
		
		$this->ipsclass->admin->output();
	}
	
	function process()
	{
		if($this->ipsclass->input['action'] == 'add')
		{
			$this->add_item_form();
		}
		else
		{
			$this->view_file_edits();
		}
	}
	
	function add_item_form()
	{
		// First get the item
		foreach($this->ipsclass->input['items'] as $item)
		{
			if($item != -1)
			{
				break;
			}
		}
		
		$class_name = $this->ipsclass->market_class->get_class_name($item);
		
		if($class_name == -1 || strstr($class_name,"item_") != $class_name || substr($item,-4) != '.php')
		{
			$this->add_item_list();
			exit();
		}
		
		$blank_item = array('item_id' => 0,'filename'=>$item);
		$this->ipsclass->market_class->load_item($blank_item);
		
		if(!is_object($this->ipsclass->market_class->items_loaded[0]))
		{
			$this->ipsclass->admin->error("ERROR: Invalid Item");
		}
		
		if(!$this->ipsclass->market_class->items_loaded[0]->allow_multiple and isset($this->ipsclass->market_class->item_by_folder[$item]))
		{
			$this->ipsclass->admin->error("ERROR: You cannot have multiple instances of this item");
		}
		
		$icon_list = $this->ipsclass->market_class->get_file_list($this->ipsclass->market_class->image_server_path."/icons/");
		$icons[0] = array('blank.gif', 'Select A Icon');
		foreach($icon_list as $file)
		{
			if(preg_match("/(.jpg|.gif|.png)/",$file)) {
				if($file != '.' || $file  != '..') {
					$icons[] = array($file,$file);
				}
			}
		}
		$image = $icons[mt_rand(0,count($icons)-1)][0];
		$icons[0] = array($image,'Select A Icon');
		$image = $this->ipsclass->market_class->image_url_path."/icons/".$image;
		
		$minute = array();
		for($i = 0; $i < 60; $i++)
		{
			$minute[$i] = array($i,$i." minutes (:".sprintf("%02u",$i).")");
		}
		$minute[1] = array(1, "1 minute (:01)");
		
		$hours = array();
		for($i = 0; $i < 24; $i++)
		{
			if($i < 12)
			{
				$hours[$i] = array($i, $i." hours (".$i." AM)");
			}
			else
			{
				$hours[$i] = array($i, $i." hours (".($i - 12)." PM)");
			}
		}
		$hours[0] = array(0, "0 hours (12 AM)");
		$hours[1] = array(1, "1 hour (1 AM)");
		$hours[24] = array(24, "24 hours (Every/Any Hour)");
		
		$days = array();
		$days[0] = array(0, "0 days (Sunday)");
		$days[1] = array(1, "1 days (Monday)");
		$days[2] = array(2, "2 days (Tuesday)");
		$days[3] = array(3, "3 days (Wednesday)");
		$days[4] = array(4, "4 days (Thursday)");
		$days[5] = array(5, "5 days (Friday)");
		$days[6] = array(6, "6 days (Saturday)");
		$days[7] = array(7, "7 days (Every/Any Day)");
		
		$groups = array();
		foreach($this->ipsclass->cache['group_cache'] as $gid => $group)
		{
			$groups[] = array($gid, $group['g_title']);
		}
		
		$this->ipsclass->admin->page_title  = "Welcome to adding a ibMarket item";
		$this->ipsclass->admin->page_detail = "Welcome to adding a ibMarket item. Here you fill out the information for the item.";
		$this->ipsclass->admin->nav[] 		= array( $this->ipsclass->form_code.'&amp;code=item&amp;do=add', 'Choose Item' );
		$this->ipsclass->admin->nav[] 		= array( ''                                                    , 'Add Item'    );
		
		$this->ipsclass->html .= $this->ipsclass->adskin->start_form( array(
																	 	  	 1 => array( 'section'   , 'components'  ),
																			 2 => array( 'act'       , 'market'      ),
																			 3 => array( 'code'      , 'item'        ),
																			 4 => array( 'do'        , 'add_item'    ),
																			 5 => array( "item[0][filename]" , $item ),
																	),'theAdminForm',"onsubmit='return ValidateForm()' enctype='multipart/form-data'");
		
		$this->ipsclass->adskin->td_header[] = array( "" , "40%" );
		$this->ipsclass->adskin->td_header[] = array( "" , "60%" );
		
		$this->ipsclass->html .= $this->ipsclass->adskin->start_table("Item Information");
		
		$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row(array(
																			"<b>Item Server Location</b>",
																			"/sources/market/items/".$item,
																	)	);
		
		$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row(array(
																			"<b>Item Default Info</b>",
																			"<b>".$this->ipsclass->market_class->items_loaded[0]->default_name."</b><br />".$this->ipsclass->parser->pre_display_parse($this->ipsclass->parser->pre_db_parse($this->ipsclass->market_class->items_loaded[0]->default_description)),
																	)	);
		
		$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row(array(
																			"<b>Item Name</b>",
																			$this->ipsclass->adskin->form_input("item[0][item_name]", $this->ipsclass->market_class->items_loaded[0]->default_name),
																	)	);
		
		$description = $this->ipsclass->market_class->items_loaded[0]->default_description;
		if ( $this->ipsclass->han_editor->method == 'rte' )
		{
			$description = $this->ipsclass->parser->convert_ipb_html_to_html( $this->ipsclass->parser->pre_display_parse( $this->ipsclass->parser->pre_db_parse( $description ) ) );
		}
		else
		{
			$description = $this->ipsclass->parser->pre_edit_parse( $description );
		}
		$this->ipsclass->html .= $this->ipsclass->adskin->add_td_basic("<b>Item Description</b><br />".$this->ipsclass->han_editor->show_editor( $description, 'item_0_description' ), 'left', 'tablerow1');
		
		$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row(array(
																			"<b>Category</b><br />Select what categoty the item should go in",
																			$this->ipsclass->adskin->form_dropdown("item[0][category]", $this->ipsclass->market_admin->build_categories_dropdown()),
																	)	);
		
		$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row(array(
																			"<b>Item Icon</b><br />Select what icon the item should be associated with",
																			$this->ipsclass->adskin->form_dropdown("item[0][icon]",$icons,"","onChange='form_change_icon(this.value,0)'") . "<img src='".$image."' name='icon_0_preview' />",
																	)	);
		
		$null = array();
		$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row(array(
																			"<b>Hide Item</b><br />Hide the item from the buying list but not from the inventory",
																			$this->ipsclass->adskin->form_yes_no("item[0][hide_item]", $this->ipsclass->market_class->items_loaded[0]->public_on_create_item($null) ? 0 : 1),
																	)	);
		
		$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row(array(
																			"<b>Item Stock</b><br />Use <i>-1</i> or <i>infinite</i> for infinite stock",
																			$this->ipsclass->adskin->form_simple_input("item[0][stock]", 0),
																	)	);
		
		$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row(array(
																			"<b>Item Base Price</b><br />The base price is the price before everything is calculated.<br />The price appearing in the buying page is the <br />base price times the category's base price multiplier.",
																			$this->ipsclass->numbers->currency_format($this->ipsclass->adskin->form_simple_input("item[0][base_price]", 0, 10)),
																	)	);
		
		$null = array();
		$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row(array(
																			"<b>Allow Item To Be Resold</b>",
																			$this->ipsclass->adskin->form_yes_no("item[0][allow_resell]", $this->ipsclass->market_class->items_loaded[0]->public_on_resold_item($null) ? 1 : 0),
																	)	);
		
		$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row(array(
																			"<b>Item Base Resell Price</b><br />The amount of money a person gets back when resold.",
																			$this->ipsclass->adskin->form_simple_input("item[0][base_resell]", 0, 10),
																	)	);
		
		$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row(array(
																			"<b>Put Item Back When Resold</b><br />When a user resell this time, should the item be put back into stock for people to buy?",
																			$this->ipsclass->adskin->form_yes_no("item[0][resell_putback]", 0),
																	)	);
		
		$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row(array(
																			"<b>Item Tax</b><br />This is added on to the category tax and the global tax. Decimal accepted.",
																			$this->ipsclass->adskin->form_simple_input("item[0][tax]", 0, 10)."%",
																	)	);
		
		$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row(array(
																			"<b>How long does the item last?</b><br />Input 0 for the item not to expire.",
																			$this->ipsclass->adskin->form_simple_input("item[0][expire_time]", 0)." ".$this->ipsclass->adskin->form_dropdown("item[0][expire_unit]", array( 0 => array('60', "Minutes"), 1 => array('3600', "Hours"), 2 => array('86400', "Days"), 3 => array('604800', "Weeks") ) ),
																	)	);
		
		$this->ipsclass->html .= $this->ipsclass->adskin->add_td_basic("Limiters", 'left', 'tablesubheader');
		
		$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row(array(
																			"<b>Max # of this Item in Inventory</b><br />The max amount of this item that could be in one's inventory. <i>0 for unlimited</i>",
																			$this->ipsclass->adskin->form_simple_input("item[0][inventory_limit]", 0),
																	)	);
		
		$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row(array(
																			"<b>Which groups cannot buy this item?</b><br />This option will limit selected groups from buying this item from the shop area only.",
																			$this->ipsclass->adskin->form_multiselect("item[0][limit_buy][]", $groups, array()),
																	)	);
		
		$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row(array(
																			"<b>Which groups cannot use this item?</b><br />This option will limit selected groups from using this item.",
																			$this->ipsclass->adskin->form_multiselect("item[0][limit_use][]", $groups, array()),
																	)	);
		
		$this->ipsclass->html .= $this->ipsclass->adskin->add_td_basic($this->ipsclass->adskin->form_input("item[0][restock_info]","",'text',"readonly='readonly' style='float:right; width:60%'")."Restock Options", 'left', 'tablesubheader');
		
		$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row(array(
																			"<b>How much to restock?</b><br />The amount to put back into stock. If 0 is put in, then there is no restocking.",
																			$this->ipsclass->adskin->form_simple_input("item[0][restock_howmuch]", 0, 'input', "onkeyup='parse_restock(0,this.name)'"),
																	)	);
		
		$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row(array(
																			"<b>When to restock?</b><br />Select the option on when the restocking occurs. If you select never or have infinite stock, the rest of the options will not take effect.",
																			$this->ipsclass->adskin->form_dropdown("item[0][restock]", array(array(0,'No Restock'), array(1,'When the stock reaches and restock after this much time'),array(2,'When the stock reaches and restock when it is this time'),array(3,'Restock every so often'),array(4,'Restock when it is this time')),0, "onchange='parse_restock(0,this.name)'"),
																	)	);
		
		$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row(array(
																			"<b>When the stock reaches?</b>: This setting is in effect if and only if 'When the stock reaches' is selected.<br /><b>When is the last time when this is restocked?</b>: This is in effect if and only if 'Restock every so often' is selected. Input Format (24-hour time): MM/DD/YYYY HH:MM",
																			$this->ipsclass->adskin->form_input("item[0][restock_when]", 0, 'input', "onkeyup='parse_restock(0,this.name)'")."<input type='button' name='item[0][restock_button]' value='Current Time' onclick='load_current_time(0)'/>",
																	)	);
		
		$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row(array(
																			"<b>If 'When the stock reaches' is selected, how much time after the stock reaches the amount above will this be restocked?</b><br /><b>If 'Every so often' is selected, how much time in between each restocking?</b><br /><b>If 'When it is this time' options is selected, what time to give the wage?</b><br /><i>Note: Time is based on GMT time zone for 'When it is this time' options.</i><br />GMT current time: ".gmdate("D, M j, Y h:i A" )."",
																			$this->ipsclass->adskin->form_dropdown("item[0][restock_day]", $days, 0, "onchange='parse_restock(0,this.name)'")."<br />".$this->ipsclass->adskin->form_dropdown("item[0][restock_hour]", $hours, 0, "onchange='parse_restock(0,this.name)'")."<br />".$this->ipsclass->adskin->form_dropdown("item[0][restock_minute]", $minute, 0, "onchange='parse_restock(0,this.name)'"),
																	)	);
		
		if($html = $this->ipsclass->market_class->items_loaded[0]->admin_form())
		{
			$this->ipsclass->html .= $this->ipsclass->adskin->add_td_basic('Extra Settings', 'left', 'tablesubheader');
			
			$this->ipsclass->html .= $html;
		}
		
		$this->ipsclass->html .= $this->ipsclass->adskin->end_form("Complete Adding a Item");
		
		$this->ipsclass->html .= $this->ipsclass->adskin->end_table();
		
		$this->ipsclass->html .= "<script type=\"text/javascript\">
//<![CDATA[
  var time_offset = ".$this->ipsclass->get_time_offset().";
  parse_restock(0,'');
//]]>
</script>
";
		
		$this->ipsclass->admin->output();
	}
	
	function add_item_finish()
	{
		if(!isset($this->ipsclass->input['item'][0]))
		{
			if(!isset($this->ipsclass->input['item']['']))
			{
				$this->add_item_list();
				exit();
			}
		}
		
		$this->ipsclass->input['item'][0] = $this->ipsclass->parse_incoming_recursively($_POST['item'][0]);
		
		$class_name = $this->ipsclass->market_class->get_class_name($this->ipsclass->input['item'][0]['filename']);
		
		if($class_name == -1 || strstr($class_name,"item_") != $class_name || substr($this->ipsclass->input['item'][0]['filename'],-4) != '.php')
		{
			$this->add_item_list();
			exit();
		}
		
		$blank_item = array('item_id' => 0,'filename'=>$this->ipsclass->input['item'][0]['filename']);
		$this->ipsclass->market_class->load_item($blank_item);
		
		if(!is_object($this->ipsclass->market_class->items_loaded[0]))
		{
			$this->ipsclass->admin->error("ERROR: Invalid Item");
		}
		
		if(!$this->ipsclass->market_class->items_loaded[0]->allow_multiple and isset($this->ipsclass->market_class->item_by_folder[$this->ipsclass->input['item'][0]['filename']]))
		{
			$this->ipsclass->admin->error("ERROR: You cannot have multiple instances of this item");
		}
		
		$this->ipsclass->input['item'][0]['item_description'] = $this->ipsclass->parser->pre_edit_parse( $this->ipsclass->parser->pre_db_parse( $this->ipsclass->han_editor->process_raw_post( 'item_0_description' ) ) );
		
		$this->ipsclass->input['item'][0]['restock'] = intval($this->ipsclass->input['item'][0]['restock']);
		if($this->ipsclass->input['item'][0]['restock'] == 1 || $this->ipsclass->input['item'][0]['restock'] == 2)
		{
			$this->ipsclass->input['item'][0]['restock_when'] = intval($this->ipsclass->input['item'][0]['restock_when']);
			$this->ipsclass->input['item'][0]['restock_when'] = max(0,$this->ipsclass->input['item'][0]['restock_when']);
		}
		elseif($this->ipsclass->input['item'][0]['restock'] == 3)
		{
			list($date, $time) = explode(" ",$this->ipsclass->input['item'][0]['restock_when']);
			list($month,$day,$year) = explode("/",$date);
			list($hour,$minute) = explode(":",$time);
			if($this->ipsclass->market_class->check_24_time($month, $day, $year, $hour, $minute))
			{
				$this->ipsclass->input['item'][0]['restock_when'] = $this->ipsclass->convert_local_date_to_unix( array( 'month'  => intval($month),
																														'day'    => intval($day),
																														'year'   => intval($year),
																														'hour'   => intval($hour),
																														'minute' => intval($minute) ) );
			}
			else
			{
				$this->ipsclass->input['item'][0]['restock_when'] = time();
				$this->ipsclass->input['item'][0]['restock_when'] = $this->ipsclass->input['item'][0]['restock_when'] - $this->ipsclass->input['item'][0]['restock_when'] % 60;
			}
		}
		else
		{
			$this->ipsclass->input['item'][0]['restock_when'] = 0;
		}
		$this->ipsclass->input['item'][0]['restock_minute'] = intval($this->ipsclass->input['item'][0]['restock_minute']);
		$this->ipsclass->input['item'][0]['restock_minute'] = min(59,max(0,$this->ipsclass->input['item'][0]['restock_minute']));
		$this->ipsclass->input['item'][0]['restock_hour'] = intval($this->ipsclass->input['item'][0]['restock_hour']);
		$this->ipsclass->input['item'][0]['restock_hour'] = min(24,max(0,$this->ipsclass->input['item'][0]['restock_hour']));
		$this->ipsclass->input['item'][0]['restock_day'] = intval($this->ipsclass->input['item'][0]['restock_day']);
		$this->ipsclass->input['item'][0]['restock_day'] = min(7,max(0,$this->ipsclass->input['item'][0]['restock_day']));
		$this->ipsclass->input['item'][0]['restock_howmuch'] = intval($this->ipsclass->input['item'][0]['restock_howmuch']);
		
		$this->ipsclass->input['item'][0]['stock'] = $this->ipsclass->input['item'][0]['stock'] == 'infinite' ? -1 : $this->ipsclass->input['item'][0]['stock'];
		
		$this->ipsclass->input['item'][0]['limit_buy'] = serialize(is_array($this->ipsclass->input['item'][0]['limit_buy']) ? $this->ipsclass->input['item'][0]['limit_buy'] : array());
		$this->ipsclass->input['item'][0]['limit_use'] = serialize(is_array($this->ipsclass->input['item'][0]['limit_use']) ? $this->ipsclass->input['item'][0]['limit_use'] : array());
		
		$item_add = array(
							'filename' => $this->ipsclass->input['item'][0]['filename'],
							'item_name' => $this->ipsclass->input['item'][0]['item_name'],
							'item_description' => $this->ipsclass->input['item'][0]['item_description'],
							'category' => intval($this->ipsclass->input['item'][0]['category']),
							'icon' => $this->ipsclass->input['item'][0]['icon'],
							'hide_item' => intval((bool)$this->ipsclass->input['item'][0]['hide_item']),
							'base_price' => $this->ipsclass->numbers->parse_input_number($this->ipsclass->input['item'][0]['base_price'],'int'),
							'base_resell' => $this->ipsclass->numbers->parse_input_number($this->ipsclass->input['item'][0]['base_resell'],'int'),
							'allow_resell' => intval((bool)$this->ipsclass->input['item'][0]['allow_resell']),
							'resell_putback' => intval((bool)$this->ipsclass->input['item'][0]['resell_putback']),
							'tax' => $this->ipsclass->numbers->parse_input_number($this->ipsclass->input['item'][0]['tax'],'float'),
							'stock' => $this->ipsclass->numbers->parse_input_number($this->ipsclass->input['item'][0]['stock'],'int'),
							'restock' => $this->ipsclass->input['item'][0]['restock'],
							'restock_when' => $this->ipsclass->input['item'][0]['restock_when'],
							'restock_minute' => $this->ipsclass->input['item'][0]['restock_minute'],
							'restock_hour' => $this->ipsclass->input['item'][0]['restock_hour'],
							'restock_day' => $this->ipsclass->input['item'][0]['restock_day'],
							'restock_howmuch' => $this->ipsclass->input['item'][0]['restock_howmuch'],
							'inventory_limit' => $this->ipsclass->numbers->parse_input_number($this->ipsclass->input['item'][0]['inventory_limit'],'int'),
							'limit_buy' => $this->ipsclass->input['item'][0]['limit_buy'],
							'limit_use' => $this->ipsclass->input['item'][0]['limit_use'],
							'expire_time' => $this->ipsclass->input['item'][0]['expire_time'],
							'expire_unit' => $this->ipsclass->input['item'][0]['expire_unit'],
						);
		
		$this->ipsclass->cache['market_items'][0] = $item_add;
		$item_add['restock_time'] = $this->ipsclass->market_class->next_restock_time(0);
		
		$this->ipsclass->market_class->items_loaded[0]->admin_save($item_add,$this->ipsclass->input['item'][0]);
		
		if(!isset($item_add['item_config']))
		{
			$item_add['item_config'] = '';
		}
		
		if(!isset($item_add['settings']))
		{
			$item_add['settings'] = '';
		}
		
		$item_add['item_description'] = $this->ipsclass->parser->pre_edit_parse($this->ipsclass->parser->pre_db_parse($item_add['item_description']));
		
		$this->ipsclass->DB->do_insert('market_items',$item_add);
		$item_id = $this->ipsclass->DB->get_insert_id();
		
		$this->ipsclass->market_class->cache_items();
		
		$this->ipsclass->admin->save_log("ibMarket: Added item ".$this->ipsclass->input['item'][0]['item_name']." (ID: ".$item_id.")");
		
		if($this->ipsclass->market_class->items_loaded[0]->admin_view_file_edits())
		{
			$this->ipsclass->admin->redirect($this->ipsclass->form_code_js."&code=item&do=edits&items[]=".$this->ipsclass->input['item'][0]['filename'],"Added item done. Redirecting you to the file edits...");
		}
		else
		{
			$this->ipsclass->admin->redirect($this->ipsclass->form_code_js."&code=item&do=list&cat=".$this->ipsclass->input['item'][0]['category'],"Added item done...");
		}
	}
	
	function mass_edit_form()
	{
		if(!is_array($this->ipsclass->input['item']))
		{
			$this->ipsclass->input['cat'] = -1;
			$this->ipsclass->input['subs'] = 1;
			if(isset($this->ipsclass->cache['market_items'][$this->ipsclass->input['item']]))
			{
				$this->ipsclass->input['cat'] = $this->ipsclass->cache['market_items'][$this->ipsclass->input['item']]['category'];
				$this->ipsclass->input['subs'] = 0;
				$this->ipsclass->input['item'] = array($this->ipsclass->input['item']=>1);
			}
			else
			{
				$this->ipsclass->input['item'] = array();
			}
		}
		if(count($this->ipsclass->input['item']) == 0)
		{
			$this->list_items();
			exit();
		}
		
		$icon_list = $this->ipsclass->market_class->get_file_list($this->ipsclass->market_class->image_server_path."/icons/");
		foreach($icon_list as $file)
		{
			if(preg_match("/(.jpg|.gif|.png)/",$file)) {
				if($file != '.' || $file  != '..') {
					$icons[] = array($file,$file);
				}
			}
		}
		
		$minute = array();
		for($i = 0; $i < 60; $i++)
		{
			$minute[$i] = array($i,$i." minutes (:".sprintf("%02u",$i).")");
		}
		$minute[1] = array(1, "1 minute (:01)");
		
		$hours = array();
		for($i = 0; $i < 24; $i++)
		{
			if($i < 12)
			{
				$hours[$i] = array($i, $i." hours (".$i." AM)");
			}
			else
			{
				$hours[$i] = array($i, $i." hours (".($i - 12)." PM)");
			}
		}
		$hours[0] = array(0, "0 hours (12 AM)");
		$hours[1] = array(1, "1 hour (1 AM)");
		$hours[24] = array(24, "24 hours (Every/Any Hour)");
		
		$days = array();
		$days[0] = array(0, "0 days (Sunday)");
		$days[1] = array(1, "1 days (Monday)");
		$days[2] = array(2, "2 days (Tuesday)");
		$days[3] = array(3, "3 days (Wednesday)");
		$days[4] = array(4, "4 days (Thursday)");
		$days[5] = array(5, "5 days (Friday)");
		$days[6] = array(6, "6 days (Saturday)");
		$days[7] = array(7, "7 days (Every/Any Day)");
		
		$groups = array();
		foreach($this->ipsclass->cache['group_cache'] as $gid => $group)
		{
			$groups[] = array($gid, $group['g_title']);
		}
		
		$js = "";
		
		$this->ipsclass->admin->page_title  = "Welcome to editing ibMarket item(s)";
		$this->ipsclass->admin->page_detail = "Welcome to editing ibMarket item(s). Here you fill out the information for the item(s).";
		$this->ipsclass->admin->nav[] 		= array( $this->ipsclass->form_code.'&amp;code=category', 'List category' );
		$this->ipsclass->admin->nav[] 		= array( ''                                             , 'List Items'    );
		$this->ipsclass->admin->nav[] 		= array( ''                                             , 'Edit Items'    );
		
		$this->ipsclass->html .= $this->ipsclass->adskin->start_form( array(
																	 	  	 1 => array( 'section'   , 'components'  ),
																			 2 => array( 'act'       , 'market'      ),
																			 3 => array( 'code'      , 'item'        ),
																			 4 => array( 'do'        , 'edit_item'   ),
																			 5 => array( 'cat'       , $this->ipsclass->input['cat']),
																			 6 => array( 'subs'      , $this->ipsclass->input['subs']),
																	),'theAdminForm',"onsubmit='return ValidateForm()' enctype='multipart/form-data'");
		
		foreach($this->ipsclass->input['item'] as $item_id => $check)
		{
			if($check != 1)
			{
				continue;
			}
			
			$js .= "  parse_restock(".$item_id.",'');\n";
			
			$this->ipsclass->cache['market_items'][$item_id]['stock'] = $this->ipsclass->cache['market_items'][$item_id]['stock'] < 0 ? 'infinite' : $this->ipsclass->cache['market_items'][$item_id]['stock'];
			
			$this->ipsclass->adskin->td_header[] = array( "" , "40%" );
			$this->ipsclass->adskin->td_header[] = array( "" , "60%" );
			
			$this->ipsclass->html .= $this->ipsclass->adskin->start_table("Item Information");
			
			$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row(array(
																				"<b>Item Server Location</b>",
																				"/sources/market/items".$this->ipsclass->cache['market_items'][$item_id]['filename'],
																		)	);
			
			$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row(array(
																				"<b>Item Default Info</b>",
																				"<b>".$this->ipsclass->market_class->items_loaded[$item_id]->default_name."</b><br />".$this->ipsclass->parser->pre_display_parse($this->ipsclass->parser->pre_db_parse($this->ipsclass->market_class->items_loaded[$item_id]->default_description)),
																		)	);
			
			$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row(array(
																				"<b>Item Name</b>",
																				$this->ipsclass->adskin->form_input("item[".$item_id."][item_name]", $this->ipsclass->cache['market_items'][$item_id]['item_name']),
																		)	);
			
			$description = $this->ipsclass->cache['market_items'][$item_id]['item_description'];
			if ( $this->ipsclass->han_editor->method == 'rte' )
			{
				$description = $this->ipsclass->parser->convert_ipb_html_to_html( $this->ipsclass->parser->pre_display_parse( $this->ipsclass->parser->pre_db_parse( $description ) ) );
			}
			else
			{
				$description = $this->ipsclass->parser->pre_edit_parse( $description );
			}
			$this->ipsclass->html .= $this->ipsclass->adskin->add_td_basic("<b>Item Description</b><br />".$this->ipsclass->han_editor->show_editor( $description, 'item_'.$item_id.'_description' ), 'left', 'tablerow1');
			
			$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row(array(
																				"<b>Category</b><br />Select what categoty the item should go in",
																				$this->ipsclass->adskin->form_dropdown("item[".$item_id."][category]", $this->ipsclass->market_admin->build_categories_dropdown(),$this->ipsclass->cache['market_items'][$item_id]['category']),
																		)	);
			
			$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row(array(
																				"<b>Item Icon</b><br />Select what icon the item should be associated with",
																				$this->ipsclass->adskin->form_dropdown("item[".$item_id."][icon]",$icons,$this->ipsclass->cache['market_items'][$item_id]['icon'],"onChange='form_change_icon(this.value,".$item_id.")'") . "<img src='".$this->ipsclass->market_class->image_url_path."/icons/".$this->ipsclass->cache['market_items'][$item_id]['icon']."' name='icon_".$item_id."_preview' />",
																		)	);
			
			$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row(array(
																				"<b>Hide Item</b><br />Hide the item from the buying list but not from the inventory",
																				$this->ipsclass->adskin->form_yes_no("item[".$item_id."][hide_item]", $this->ipsclass->cache['market_items'][$item_id]['hide_item']),
																		)	);
			
			$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row(array(
																				"<b>Item Stock</b><br />Use <i>-1</i> or <i>infinite</i> for infinite stock",
																				$this->ipsclass->adskin->form_simple_input("item[".$item_id."][stock]", $this->ipsclass->cache['market_items'][$item_id]['stock']),
																		)	);
			
			$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row(array(
																				"<b>Item Base Price</b><br />The base price is the price before everything is calculated.<br />The price appearing in the buying page is the <br />base price times the category's base price multiplier.",
																				$this->ipsclass->numbers->currency_format($this->ipsclass->adskin->form_simple_input("item[".$item_id."][base_price]", $this->ipsclass->cache['market_items'][$item_id]['base_price'],10)),
																		)	);
			
			$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row(array(
																				"<b>Allow Item To Be Resold</b>",
																				$this->ipsclass->adskin->form_yes_no("item[".$item_id."][allow_resell]", $this->ipsclass->cache['market_items'][$item_id]['allow_resell']),
																		)	);
			
			$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row(array(
																				"<b>Item Base Resell Price</b><br />The amount of money a person gets back.",
																				$this->ipsclass->adskin->form_simple_input("item[".$item_id."][base_resell]", $this->ipsclass->cache['market_items'][$item_id]['base_resell'],10),
																		)	);
			
			$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row(array(
																				"<b>Put Item Back When Resold</b><br />When a user resell this time, should the item be put back into stock for people to buy?",
																				$this->ipsclass->adskin->form_yes_no("item[".$item_id."][resell_putback]", $this->ipsclass->cache['market_items'][$item_id]['resell_putback']),
																		)	);
			
			$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row(array(
																				"<b>Item Tax</b><br />This is added on to the category tax and the global tax. Decimal accepted.",
																				$this->ipsclass->adskin->form_simple_input("item[".$item_id."][tax]", $this->ipsclass->numbers->clean_number($this->ipsclass->cache['market_items'][$item_id]['tax']),10)."%",
																		)	);
			
			$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row(array(
																				"<b>How long does the item last?</b><br />Input 0 for the item not to expire.",
																				$this->ipsclass->adskin->form_simple_input("item[".$item_id."][expire_time]", $this->ipsclass->cache['market_items'][$item_id]['expire_time'])." ".$this->ipsclass->adskin->form_dropdown("item[".$item_id."][expire_unit]", array( 0 => array('60', "Minutes"), 1 => array('3600', "Hours"), 2 => array('86400', "Days"), 3 => array('604800', "Weeks") ), $this->ipsclass->cache['market_items'][$item_id]['expire_unit'] ),
																		)	);
			
			$this->ipsclass->html .= $this->ipsclass->adskin->add_td_basic("Limiters", 'left', 'tablesubheader');
			
			$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row(array(
																				"<b>Max # of this Item in Inventory</b><br />The max amount of this item that could be in one's inventory. <i>0 for unlimited</i>",
																				$this->ipsclass->adskin->form_simple_input("item[".$item_id."][inventory_limit]", $this->ipsclass->cache['market_items'][$item_id]['inventory_limit']),
																		)	);
			
			$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row(array(
																				"<b>Which groups cannot buy this item?</b><br />This option will limit selected groups from buying this item from the shop area only.",
																				$this->ipsclass->adskin->form_multiselect("item[".$item_id."][limit_buy][]", $groups, $this->ipsclass->cache['market_items'][$item_id]['limit_buy']),
																		)	);
			
			$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row(array(
																				"<b>Which groups cannot use this item?</b><br />This option will limit selected groups from using this item.",
																				$this->ipsclass->adskin->form_multiselect("item[".$item_id."][limit_use][]", $groups, $this->ipsclass->cache['market_items'][$item_id]['limit_use']),
																		)	);
			
			$this->ipsclass->html .= $this->ipsclass->adskin->add_td_basic($this->ipsclass->adskin->form_input("item[".$item_id."][restock_info]","",'text',"readonly='readonly' style='float:right; width:60%'")."Restock Options", 'left', 'tablesubheader');
			
			$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row(array(
																				"<b>How much to restock?</b><br />The amount to put back into stock. If 0 is put in, then there is no restocking.",
																				$this->ipsclass->adskin->form_simple_input("item[".$item_id."][restock_howmuch]", $this->ipsclass->cache['market_items'][$item_id]['restock_howmuch'], 'input', "onkeyup='parse_restock(".$item_id.",this.name)'"),
																		)	);
			
			$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row(array(
																				"<b>When to restock?</b><br />Select the option on when the restocking occurs. If you select never or have infinite stock, the rest of the options will not take effect.",
																				$this->ipsclass->adskin->form_dropdown("item[".$item_id."][restock]", array(array(0,'No Restock'), array(1,'When the stock reaches and restock after this much time'),array(2,'When the stock reaches and restock when it is this time'),array(3,'Restock every so often'),array(4,'Restock when it is this time')), $this->ipsclass->cache['market_items'][$item_id]['restock'], "onchange='parse_restock(".$item_id.",this.name)'"),
																		)	);
			
			$default = "0";
			if($this->ipsclass->cache['market_items'][$item_id]['restock'] == 1 || $this->ipsclass->cache['market_items'][$item_id]['restock'] == 2)
			{
				$default = $this->ipsclass->cache['market_items'][$item_id]['restock_when'];
			}
			elseif($this->ipsclass->cache['market_items'][$item_id]['restock'] == 3)
			{
				$date = $this->ipsclass->unixstamp_to_human($this->ipsclass->cache['market_items'][$item_id]['restock_when']);
				$default = sprintf("%02d/%02d/%04d %02d:%02d", $date['month'], $date['day'], $date['year'], $date['hour'] , $date['minute'] );
			}
			
			$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row(array(
																				"<b>When the stock reaches?</b>: This setting is in effect if and only if 'When the stock reaches' is selected.<br /><b>When is the last time when this is restocked?</b>: This is in effect if and only if 'Restock every so often' is selected. Input Format (24-hour time): MM/DD/YYYY HH:MM",
																				$this->ipsclass->adskin->form_input("item[".$item_id."][restock_when]", $default, 'input', "onkeyup='parse_restock(".$item_id.",this.name)'")."<input type='button' name='item[".$item_id."][restock_button]' value='Current Time' onclick='load_current_time(".$item_id.")'/>",
																		)	);
			
			$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row(array(
																				"<b>If 'When the stock reaches' is selected, how much time after the stock reaches the amount above will this be restocked?</b><br /><b>If 'Every so often' is selected, how much time in between each restocking?</b><br /><b>If 'When it is this time' options is selected, what time to give the wage?</b><br /><i>Note: Time is based on GMT time zone for 'When it is this time' options.</i><br />GMT current time: ".gmdate("D, M j, Y h:i A" )."",
																				$this->ipsclass->adskin->form_dropdown("item[".$item_id."][restock_day]", $days, $this->ipsclass->cache['market_items'][$item_id]['restock_day'], "onchange='parse_restock(".$item_id.",this.name)'")."<br />".$this->ipsclass->adskin->form_dropdown("item[".$item_id."][restock_hour]", $hours, $this->ipsclass->cache['market_items'][$item_id]['restock_hour'], "onchange='parse_restock(".$item_id.",this.name)'")."<br />".$this->ipsclass->adskin->form_dropdown("item[".$item_id."][restock_minute]", $minute, $this->ipsclass->cache['market_items'][$item_id]['restock_minute'], "onchange='parse_restock(".$item_id.",this.name)'"),
																		)	);
			
			if($html = $this->ipsclass->market_class->items_loaded[$item_id]->admin_form())
			{
				$this->ipsclass->html .= $this->ipsclass->adskin->add_td_basic('Extra Settings', 'left', 'tablesubheader');
				
				$this->ipsclass->html .= $html;
			}
			
			$this->ipsclass->html .= $this->ipsclass->adskin->end_table();
		}
		
		$this->ipsclass->html .= $this->ipsclass->adskin->end_form_standalone("Complete Editing Item(s)");
		
		$this->ipsclass->html .= "<script type=\"text/javascript\">
//<![CDATA[
  var time_offset = ".$this->ipsclass->get_time_offset().";
".$js."
//]]>
</script>
";
		
		$this->ipsclass->admin->output();
		
	}
	
	function mass_edit_save()
	{
		if(!is_array($this->ipsclass->input['item']) || count($this->ipsclass->input['item']) == 0)
		{
			$this->list_items();
		}
		
		$update = array();
		foreach($_POST['item'] as $item_id => $input)
		{
			$input = $this->ipsclass->parse_incoming_recursively($input);
			
			$input['item_description'] = $this->ipsclass->parser->pre_edit_parse( $this->ipsclass->parser->pre_db_parse( $this->ipsclass->han_editor->process_raw_post( 'item_'.$item_id.'_description' ) ) );
			
			$input['restock'] = intval($input['restock']);
			if($input['restock'] == 1 || $input['restock'] == 2)
			{
				$input['restock_when'] = intval($input['restock_when']);
				$input['restock_when'] = max(0,$input['restock_when']);
			}
			elseif($input['restock'] == 3)
			{
				
				list($date, $time) = explode(" ",$input['restock_when']);
				list($month,$day,$year) = explode("/",$date);
				list($hour,$minute) = explode(":",$time);
				if($this->ipsclass->market_class->check_24_time($month, $day, $year, $hour, $minute))
				{
					$input['restock_when'] = $this->ipsclass->convert_local_date_to_unix( array( 'month'  => intval($month),
																								 'day'    => intval($day),
																								 'year'   => intval($year),
																								 'hour'   => intval($hour),
																								 'minute' => intval($minute) ) );
				}
				else
				{
					$input['restock_when'] = $this->ipsclass->cache['market_items'][$item_id]['restock_when'];
				}
			}
			else
			{
				$input['restock_when'] = 0;
			}
			$input['restock_minute'] = intval($input['restock_minute']);
			$input['restock_minute'] = min(59,max(0,$input['restock_minute']));
			$input['restock_hour'] = intval($input['restock_hour']);
			$input['restock_hour'] = min(24,max(0,$input['restock_hour']));
			$input['restock_day'] = intval($input['restock_day']);
			$input['restock_day'] = min(7,max(0,$input['restock_day']));
			$input['restock_howmuch'] = intval($input['restock_howmuch']);
			
			$input['stock'] = $input['stock'] == 'infinite' ? -1 : $input['stock'];
			
			$input['limit_buy'] = serialize(is_array($input['limit_buy']) ? $input['limit_buy'] : array());
			$input['limit_use'] = serialize(is_array($input['limit_use']) ? $input['limit_use'] : array());
			
			$item_add = array(
								'item_name' => $input['item_name'],
								'item_description' => $input['item_description'],
								'category' => intval($input['category']),
								'icon' => $input['icon'],
								'hide_item' => intval((bool)$input['hide_item']),
								'base_price' => $this->ipsclass->numbers->parse_input_number($input['base_price'],'int'),
								'base_resell' => $this->ipsclass->numbers->parse_input_number($input['base_resell'],'int'),
								'stock' => $this->ipsclass->numbers->parse_input_number($input['stock'],'int'),
								'allow_resell' => intval((bool)$input['allow_resell']),
								'resell_putback' => intval((bool)$input['resell_putback']),
								'tax' => $this->ipsclass->numbers->parse_input_number($input['tax'],'float'),
								'restock' => $input['restock'],
								'restock_when' => $input['restock_when'],
								'restock_minute' => $input['restock_minute'],
								'restock_hour' => $input['restock_hour'],
								'restock_day' => $input['restock_day'],
								'restock_howmuch' => $input['restock_howmuch'],
								'inventory_limit' => $this->ipsclass->numbers->parse_input_number($input['inventory_limit'],'int'),
								'limit_buy' => $input['limit_buy'],
								'limit_use' => $input['limit_use'],
								'expire_time' => $input['expire_time'],
								'expire_unit' => $input['expire_unit'],
							);
			
			$this->ipsclass->cache['market_items'][-1] = $item_add;
			$item_add['restock_time'] = $this->ipsclass->market_class->next_restock_time(-1);
			
			$this->ipsclass->market_class->items_loaded[$item_id]->admin_save($item_add,$input);
			
			$item_add['item_description'] = $this->ipsclass->parser->pre_edit_parse($this->ipsclass->parser->pre_db_parse($item_add['item_description']));
			
			$update[$item_id] = $item_add;
		}
		
		foreach($update as $item_id => $item_add)
		{
			$this->ipsclass->DB->do_update('market_items',$item_add,"item_id=".$item_id);
			
			$this->ipsclass->admin->save_log("ibMarket: Editted item ".$_POST['item'][$item_id]['item_name']." (ID: ".$item_id.")");
		}
		$this->ipsclass->market_class->cache_items();
		
		$this->ipsclass->admin->redirect($this->ipsclass->form_code_js."&code=item&do=list&cat=".$this->ipsclass->input['cat']."&subs=".$this->ipsclass->input['subs'],"Edited item(s) done...");
	}
	
	function delete_form()
	{
		$this->ipsclass->input['item'] = intval($this->ipsclass->input['item']);
		
		$this->ipsclass->admin->page_title  = "Welcome to delete a ibMarket category";
		$this->ipsclass->admin->page_detail = "Welcome to delete a ibMarket category.";
		$this->ipsclass->admin->nav[] 		= array( $this->ipsclass->form_code.'&amp;code=category', 'List category' );
		$this->ipsclass->admin->nav[] 		= array( ''                                             , 'List Items'    );
		$this->ipsclass->admin->nav[] 		= array( ''                                             , 'Delete Item'   );
		
		$this->ipsclass->html .= $this->ipsclass->adskin->start_form( array(
																	 	  	 1 => array( 'section'   , 'components'      ),
																			 2 => array( 'act'       , 'market'          ),
																			 3 => array( 'code'      , 'item'            ),
																			 4 => array( 'do'        , 'delete_item'     ),
																			 5 => array( 'item'      , $this->ipsclass->input['item']),
																	)		);
		
		$this->ipsclass->adskin->td_header[] = array( "" , "" );
		$this->ipsclass->adskin->td_header[] = array( "" , "" );
		
		$this->ipsclass->html .= $this->ipsclass->adskin->start_table("Required");
		
		$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row(array(
																			"<b>Item Name</b>",
																			$this->ipsclass->cache['market_items'][$this->ipsclass->input['item']]['item_name'],
																	)	);
		
		$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row(array(
																			"<b>Remove the items from inventory and:</b>",
																			$this->ipsclass->adskin->form_dropdown('what_to_do', array(array(0, "Without paying back"),array(1, "Paying the resell price"),array(2, "Paying back bought price"),array(3, "Paying back with current price (no taxes)"),array(4, "Paying back with current price with all taxes"))),
																	)	);
		
		$this->ipsclass->html .= $this->ipsclass->adskin->end_form("Remove Item");
		
		$this->ipsclass->html .= $this->ipsclass->adskin->end_table();
		
		$this->ipsclass->admin->output();
	}
	
	function delete_item()
	{
		$this->ipsclass->input['item'] = intval($this->ipsclass->input['item']);
		$this->ipsclass->input['what_to_do'] = intval($this->ipsclass->input['what_to_do']);
		
		if($this->ipsclass->input['what_to_do'])
		{
			$LIMIT = 100;
			$this->ipsclass->input['st'] = intval($this->ipsclass->input['st']);
			
			$update = array();
			
			$this->ipsclass->DB->build_query(array('select'=>'*','from'=>'market_inventory','where'=>"item=".$this->ipsclass->input['item'],'order'=>'member_id ASC','limit'=>array($this->ipsclass->input['st'],$LIMIT)));
			$this->ipsclass->DB->exec_query();
			while($inven = $this->ipsclass->DB->fetch_row())
			{
				if($this->ipsclass->input['what_to_do'] == 1)
				{
					$update[$inven['member_id']] = $this->ipsclass->numbers->add($update[$inven['member_id']],$inven['resell_price']);
				}
				elseif($this->ipsclass->input['what_to_do'] == 2)
				{
					$update[$inven['member_id']] = $this->ipsclass->numbers->add($update[$inven['member_id']],$inven['price_bought']);
				}
				elseif($this->ipsclass->input['what_to_do'] == 3)
				{
					$update[$inven['member_id']] = $this->ipsclass->numbers->add($update[$inven['member_id']],$this->ipsclass->numbers->multiply($this->ipsclass->cache['market_items'][$this->ipsclass->input['item']]['base_price'],$this->ipsclass->cache['market_category'][$this->ipsclass->cache['market_items'][$this->ipsclass->input['item']]['category']]['price_multiplier']));
				}
				elseif($this->ipsclass->input['what_to_do'] == 4)
				{
					$update[$inven['member_id']] = $this->ipsclass->numbers->add($update[$inven['member_id']],$this->ipsclass->numbers->multiply($this->ipsclass->cache['market_items'][$this->ipsclass->input['item']]['base_price'],$this->ipsclass->cache['market_category'][$this->ipsclass->cache['market_items'][$this->ipsclass->input['item']]['category']]['price_multiplier'],$this->ipsclass->numbers->divide($this->ipsclass->numbers->add(100,$this->ipsclass->vars['market_global_tax'],$this->ipsclass->cache['market_category'][$this->ipsclass->cache['market_items'][$this->ipsclass->input['item']]['category']]['cat_tax']),100)));
				}
			}
			
			if(count($update))
			{
				foreach($update as $member_id => $add_money)
				{
					$this->ipsclass->DB->build_and_exec_query(array('update'=>'members','set'=>$this->ipsclass->vars['market_members_field']."=".$this->ipsclass->vars['market_members_field']."+".$add_money,'where'=>"id=".$member_id));
				}
				
				$this->ipsclass->admin->redirect($this->ipsclass->form_code_js."&code=item&do=delete_item&item=".$this->ipsclass->input['item']."&what_to_do=".$this->ipsclass->input['what_to_do']."&st=".($this->ipsclass->input['st']+$LIMIT),"Processing delete item...");
			}
		}
		
		$this->ipsclass->market_class->items_loaded[$this->ipsclass->input['item']]->admin_remove();
		$this->ipsclass->DB->build_and_exec_query(array('delete'=>'market_inventory','where'=>"item=".$this->ipsclass->input['item']));
		$this->ipsclass->DB->build_and_exec_query(array('delete'=>'market_items','where'=>"item_id=".$this->ipsclass->input['item']));
		
		$this->ipsclass->admin->save_log("ibMarket: Deleted item ".$this->ipsclass->cache['market_items'][$this->ipsclass->input['item']]['item_name']." (ID: ".$this->ipsclass->input['item'].")");
		
		$this->ipsclass->market_class->cache_items();
		
		$this->ipsclass->admin->redirect($this->ipsclass->form_code_js."&code=item","Removed item done...");
	}
	
	function view_file_edits()
	{
		// First get the item
		foreach($this->ipsclass->input['items'] as $item)
		{
			if($item != -1)
			{
				break;
			}
		}
		
		$class_name = $this->ipsclass->market_class->get_class_name($item);
		
		if($class_name == -1 || strstr($class_name,"item_") != $class_name || substr($item,-4) != '.php')
		{
			$this->add_item_list();
			exit();
		}
		
		$blank_item = array('item_id' => 0,'filename'=>$item);
		$this->ipsclass->market_class->load_item($blank_item);
		
		$this->ipsclass->admin->page_title  = "Welcome to viewing file edits to a specific item";
		$this->ipsclass->admin->page_detail = "Welcome to viewing file edits to a specific item.<br /><i><b>Warning</b></i>: Please back up your files before applying the edits.";
		$this->ipsclass->admin->nav[] 		= array( $this->ipsclass->form_code.'&amp;code=item&amp;do=add', 'Choose Item'     );
		$this->ipsclass->admin->nav[] 		= array( ''                                                    , 'View Item Edits' );
		
		$this->ipsclass->html .= $this->ipsclass->adskin->start_table("Item Information");
		
		$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row(array(
																			"<b>Item Server Location</b>",
																			"/sources/market/items/".$item,
																	)	);
		
		$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row(array(
																			"<b>Item Default Info</b>",
																			"<b>".$this->ipsclass->market_class->items_loaded[0]->default_name."</b><br />".$this->ipsclass->parser->pre_display_parse($this->ipsclass->market_class->items_loaded[0]->default_description),
																	)	);
		
		$this->ipsclass->html .= $this->ipsclass->adskin->end_table();
		
		$this->ipsclass->html .= $this->ipsclass->market_class->items_loaded[0]->admin_view_file_edits();
		
		$this->ipsclass->admin->output();
	}
	
	function quick_save_items()
	{
		if(!is_array($this->ipsclass->input['item']) || count($this->ipsclass->input['item']) == 0)
		{
			$this->list_items();
		}
		
		$update = array();
		foreach($this->ipsclass->input['item'] as $item_id => $input)
		{
			$input['stock'] = $input['stock'] == 'infinite' ? -1 : $input['stock'];
			
			if($this->ipsclass->cache['market_items'][$item_id]['base_price'] == $input['base_price'] && $this->ipsclass->cache['market_items'][$item_id]['stock'] == $input['stock'])
			{
				continue;
			}
			
			$item_edit = array(
								'base_price' => $this->ipsclass->numbers->parse_input_number($input['base_price'],'int'),
								'stock' => $this->ipsclass->numbers->parse_input_number($input['stock'],'int'),
							);
			
			$this->ipsclass->market_class->items_loaded[$item_id]->admin_quick_save($item_edit,'stockprice');
			
			$update[$item_id] = $item_edit;
		}
		
		foreach($update as $item_id => $item_edit)
		{
			$this->ipsclass->DB->do_update('market_items',$item_edit,"item_id=".$item_id);
			
			$this->ipsclass->admin->save_log("ibMarket: Quick editted item ".$this->ipsclass->cache['market_items'][$item_id]['item_name']." (ID: ".$item_id.")");
		}
		
		$this->ipsclass->market_class->cache_items();
		
		$this->ipsclass->admin->redirect($this->ipsclass->form_code_js."&code=item&do=quick&cat=".$this->ipsclass->input['cat']."&subs=".$this->ipsclass->input['subs'],"Edited item(s) done...");
	}
}
?>